#include <assert.h>
#include "epoll.h"
#include "udp.h"

int main(void)
{
	Udp server;
	Epoll ep;

	assert( ep.Open(100) );
	assert( server.ReuseAddress(true) );
	assert( server.Bind("0.0.0.0", 10000) );
	assert( ep.AddEvent(server.GetSocket(), EPOLLIN) );

	while(1)
	{
		char buf[1024] = {0};
		int len = sizeof(buf);
		int count = ep.WaitEvent(1);
		for(int i = 0; i < count; i++)
		{
			struct epoll_event &e = ep[i];
			if( EPOLLIN & e.events )
			{
				string ip;
				int port = 0;
				len = server.Recv(ip, port, buf, len);
				if( len > 0 )
				{
					buf[ len ] = 0;
					printf("%s:%d[%s]=%d\n", ip.data(), port, buf, len);
				}
				else
				{
					ep.DelEvent(e.data.fd, EPOLLIN|EPOLLOUT);
					server.Close();
				}
			}
			if( EPOLLPRI & e.events )
			{
				server.RecvOob(buf, len);
			}
			if( EPOLLHUP & e.events )
			{
				ep.DelEvent(e.data.fd, EPOLLIN|EPOLLOUT);
				server.Close();
			}
		}
		usleep(100);
	}

	return 0;
}
